package com.hunttown.mes.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * created by hunttown on 2017/7/27
 */
public class PageUtils {
    //组装page.vm的相关数据信息
    public static Map<String, Object> PagePackage(int pn, int start, int pageSize, int totalPages, Long totalItems) {
        Map<String, Object> page = new HashMap<>();

        int fromItem = start + 1;
        page.put("fromItem", fromItem);

        Long toItem = (long) start + (long) pageSize;
        toItem = toItem > totalItems ? totalItems : toItem;
        page.put("toItem", toItem);

        int prePn = pn - 1;
        prePn = prePn <= 0 ? 1 : prePn;
        page.put("prePn", prePn);

        int nextPn = pn + 1;
        nextPn = nextPn > totalPages ? totalPages : nextPn;
        page.put("nextPn", nextPn);

        // 确定页码的上界和下界 显示10页
        int firtPage = 1;
        int lastPage = totalPages;
        if (totalPages > 10) {
            firtPage = pn - 4;
            lastPage = pn + 5;

            if (firtPage <= 0) {
                firtPage = 1;
                lastPage = firtPage + 9;
            }

            if (lastPage > totalPages) {
                lastPage = totalPages;
                firtPage = lastPage - 9;
            }
        }

        int[] pageArr = new int[lastPage - firtPage + 1];
        int j = 0;
        for (int i = firtPage; i <= lastPage; i++) {
            pageArr[j] = i;
            j++;
        }
        page.put("pageArr", pageArr);

        return page;
    }
}
